<template>
  <div class="home-content">
    <h1 class="title">生鲜超市</h1>
    <div class="category-container">
      <div class="category-item" @click="$router.push('/meat')">
        <img src="../assets/images/meat.png" alt="鲜肉" class="category-img">
        <span class="category-name">鲜肉</span>
      </div>
      <div class="category-item" @click="$router.push('/vegetable')">
        <img src="../assets/images/vegetable.png" alt="蔬菜" class="category-img">
        <span class="category-name">蔬菜</span>
      </div>
      <div class="category-item" @click="$router.push('/fruit')">
        <img src="../assets/images/fruit.png" alt="水果" class="category-img">
        <span class="category-name">水果</span>
      </div>
      <div class="category-item" @click="$router.push('/others')">
        <img src="../assets/images/others.png" alt="其他" class="category-img">
        <span class="category-name">其他</span>
      </div>
    </div>

    <!-- 推荐商品轮播图 -->
    <div class="carousel-container">
      <h2 class="section-title">推荐商品</h2>
      <el-carousel :interval="4000" type="card" height="300px">
        <el-carousel-item v-for="(item, index) in carouselItems" :key="index" @click="handleCarouselClick(item.id)">
          <img :src="item.image" :alt="'推荐商品' + (index + 1)" class="carousel-img">
        </el-carousel-item>
      </el-carousel>
    </div>
    <TopProductsView />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import TopProductsView from './product/TopProductsView.vue'

const router = useRouter()

// 轮播图数据
const carouselItems = ref([
  { id: 3, image: new URL('../assets/images/lb/1.png', import.meta.url).href },
  { id: 14, image: new URL('../assets/images/lb/2.png', import.meta.url).href },
  { id: 43, image: new URL('../assets/images/lb/3.png', import.meta.url).href },
  { id: 42, image: new URL('../assets/images/lb/4.png', import.meta.url).href }
])

// 处理轮播图点击
const handleCarouselClick = (productId) => {
  router.push(`/product/${productId}`)
}

// 获取分类列表
const getCategories = async () => {
  const { data } = await getCategories()
  return data
}
</script>

<style scoped>
.home-content {
  width: 100%;
}

.title {
  font-size: 36px;
  margin-bottom: 30px;
  color: #2c3e50;
  text-align: center;
  font-weight: 600;
}

.category-container {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 30px;
  padding: 20px 0;
}

.category-item {
  background: white;
  padding: 20px;
  border-radius: 12px;
  text-align: center;
  transition: all 0.3s ease;
  cursor: pointer;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.category-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.category-img {
  width: 100%;
  height: 180px;
  object-fit: cover;
  border-radius: 8px;
  margin-bottom: 15px;
  transition: transform 0.3s ease;
}

.category-item:hover .category-img {
  transform: scale(1.05);
}

.category-name {
  display: block;
  font-size: 18px;
  color: #2c3e50;
  font-weight: 500;
  margin-top: 10px;
}

/* 轮播图样式 */
.carousel-container {
  margin-top: 40px;
  padding: 20px 0;
}

.section-title {
  font-size: 28px;
  color: #2c3e50;
  text-align: center;
  margin-bottom: 20px;
  font-weight: 600;
}

.carousel-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
  cursor: pointer;
}

:deep(.el-carousel__item) {
  border-radius: 8px;
  overflow: hidden;
}

@media (max-width: 1200px) {
  .category-container {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style> 